#!/user/bin/env python
# -*- coding:utf-8 -*-
#filename:controllers/tfeetsearchjson.py

'''
Created on 2011-9-6

@author: zc


modify on 2011-09-12  None json--> {}

'''

from dbhandle import mysqldb
from config import sqlmap
from config.settings import NEXTFEET

class tfeetsearchjson():
    __dbo = mysqldb.mysqldb()

    def __init__( self ):
        pass


    def tfeetsearchjson( self, srchcontent, tfid = 0 ):
        ''' 获取tfeet数据并组合成json形式返回
            http://tinyfeet.us/i/getfeet/all?postart=0&srckey=2a2286d27ee79ecdf39ee0266e485f578aa39d2fecunrxwy
            '''
        
        if len( srchcontent ) > 0:
            if tfid == 0:
                print """ 第一次"""  
                sql_tfeet = sqlmap.SQL_SEARCH1ST % ( srchcontent, srchcontent, NEXTFEET )
            elif tfid > 0:
                print """ 第N次 N>=2"""
                sql_tfeet = sqlmap.SQL_SEARCHMANY % ( srchcontent, srchcontent, tfid, NEXTFEET )


        
        if len( sql_tfeet ) > 0: 
            tfeets_res = self.__dbo.query( sql_tfeet )
        
        """  """
        tfeets = None
                           
        if tfeets_res:
            
            tfeets = '{"tfeets":['
            
            for tf in tfeets_res:
                commentCnt = self.getCommentCnt( tf.tfid )
                tfeets = tfeets + ( '''{
                                    "uname" : "''' + tf.uname + '''",
                                    "uid"   : "''' + str( tf.uid ) + '''",
                                    "aboutme": "''' + tf.aboutme + '''",
                                    "avator" :"''' + tf.avator + '''",
                                    "tfeet":
                                    {
                                        "tfid"  :"''' + str( tf.tfid ) + '''",
                                        "text"  :"''' + tf.content + '''",
                                        "image" :"''' + tf.images + '''",
                                        "datetime" :"''' + str( tf.date ) + '''",
                                        "comments" :"''' + str( commentCnt ) + '''",
                                        "location" :"''' + tf.place + '''",
                                        "long" : "''' + str( tf.longitude ) + '''",
                                        "lat"  :"''' + str( tf.latigude ) + '''",
                                        "like" :"''' + str( tf.like ) + '''",
                                        "dislike" :"''' + str( tf.dislike ) + '''"
                                        },
                                    "sort" : "desc"
                            },''' )
                        
            tfeets = tfeets[:-1] #去掉最后一个逗号
            
            tfeets = tfeets + "]}"
            
        if not tfeets:
            tfeets = "{}"
        return tfeets
    
                
    def getCommentCnt( self, tfid ):
        """ 根据tfid获取该tfeet评论数 """
        sql_commentcount = sqlmap.SQL_GETCOMMENTSNUM % ( tfid )
        cres = self.__dbo.query( sql_commentcount )
        if cres:
            for tff in cres:
                commentcnt = tff.cnt
        else:
            commentcnt = 0
            
        return commentcnt



if __name__ == "__main__":
    """ 测试 """
    dsd = "钓鱼"
    t = tfeetsearchjson()
    s = t.tfeet_json( dsd )
    print s
    
